//
//  PublishCommentController.m
//  collection
//
//  Created by 靖 张 on 11/26/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import "PublishCommentController.h"

@interface PublishCommentController()

@property (nonatomic, strong) IBOutlet  UIImageView* titleBgPic;
@property (nonatomic, strong) IBOutlet  UITextView* editView;
@property (nonatomic, strong) IBOutlet  UIView*     referenceView;
@property (nonatomic, strong) IBOutlet  UIView*     additionFunctionView;

- (IBAction)onClickSendButton:(id)sender;
- (IBAction)onClickCancelButton:(id)sender;
- (IBAction)onSwitchOption:(id)sender;

- (void)keyboardWillShow:(NSNotification*) notification;
- (void)keyboardWillHide:(NSNotification*) notification;

@end

@implementation PublishCommentController

@synthesize isCommentOrForward = _isCommentOrForward;
@synthesize sourceFeed = _sourceFeed;
@synthesize titleBgPic = _titleBgPic;
@synthesize editView = _editView;
@synthesize referenceView = _referenceView;
@synthesize additionFunctionView = _additionFunctionView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        // Register keyboard event handler
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    return self;
}

- (void)dealloc {
    // Unregister keyboard event handler
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    self.editView = nil;
    self.referenceView = nil;
    self.additionFunctionView = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // Title
    if (self.isCommentOrForward) {
        self.titleBgPic.image = [UIImage imageNamed:@"top_comment_on"];
        UILabel* switchHintLabel = (UILabel*)[self.additionFunctionView viewWithTag:2];
        switchHintLabel.text = @"同时转发";
    }
    else {
        self.titleBgPic.image = [UIImage imageNamed:@"top_zhuan_on"];
        UILabel* switchHintLabel = (UILabel*)[self.additionFunctionView viewWithTag:2];
        switchHintLabel.text = @"同时评论";
    }
    
    [self.editView becomeFirstResponder];
    
    if (self.sourceFeed != nil) {
        UILabel* sourceText = (UILabel*)[self.referenceView viewWithTag:2];
        sourceText.text = [NSString stringWithFormat:@"原文：%@", self.sourceFeed.text];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.editView = nil;
    self.referenceView = nil;
    self.additionFunctionView = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Keyboard event handler

- (void)keyboardWillShow:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    
    NSValue* value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    CGRect frame;
    // Adjust addition function view
    frame = self.additionFunctionView.frame;
    frame.origin.y -= keyboardSize.height;
    self.additionFunctionView.frame = frame;
    
    // Adjust reference view
    frame = self.referenceView.frame;
    frame.origin.y -= keyboardSize.height;
    self.referenceView.frame = frame;
    
    // Adjust the size of edit view
    frame = self.editView.frame;
    frame.size.height -= keyboardSize.height;
    self.editView.frame = frame;
}

- (void)keyboardWillHide:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    CGRect frame;
    // Adjust addition function view
    frame = self.additionFunctionView.frame;
    frame.origin.y += keyboardSize.height;
    self.additionFunctionView.frame = frame;
    
    // Adjust reference view
    frame = self.referenceView.frame;
    frame.origin.y += keyboardSize.height;
    self.referenceView.frame = frame;
    
    // Adjust the size of edit view
    frame = self.editView.frame;
    frame.size.height += keyboardSize.height;
    self.editView.frame = frame;
}

#pragma mark - Do or cancel sending comment

- (IBAction)onClickSendButton:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)onClickCancelButton:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)onSwitchOption:(id)sender {
    UIButton* switchButton = (UIButton*)sender;
    switchButton.selected = !switchButton.selected;
}

@end
